package com.ruicar.afs.cloud.account.common.processor.impl.msg;

import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.account.common.entity.VoucherFlowInfo;
import com.ruicar.afs.cloud.account.common.processor.AbsBaseProcessor;
import com.ruicar.afs.cloud.account.common.processor.IRequest;
import com.ruicar.afs.cloud.account.common.processor.enums.VoucherMsgType;
import com.ruicar.afs.cloud.account.common.processor.impl.VoucherItemRequest;
import com.ruicar.afs.cloud.account.common.processor.impl.VoucherResponse;
import com.ruicar.afs.cloud.account.common.service.VoucherFlowInfoService;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.modules.contract.enums.VoucherSendStatus;
import com.ruicar.afs.cloud.voucher.dto.VoucherItemDto;
import com.ruicar.afs.cloud.voucher.feign.VoucherItemApiFeign;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
@AllArgsConstructor
public class VoucherItemSendProcessor extends AbsBaseProcessor<VoucherItemRequest, VoucherResponse> {
    private final VoucherItemApiFeign voucherItemApiFeign;
    private final VoucherFlowInfoService voucherFlowInfoService;

    @Override
    public VoucherMsgType msgType() {
        return VoucherMsgType.CONFIDENTIAL_INFORMATION;
    }
    @Override
    public List process(VoucherItemRequest request) {
        //发送凭证辅助帐信息
        VoucherResponse response = this.callServer(request);
        VoucherFlowInfo voucherFlowInfo=request.getVoucherFlowInfo();
            if(CommonConstants.SUCCESS.equals(response.getCode())){
                voucherFlowInfo.setItemStatus(VoucherSendStatus.SEND_SUCCESS);
                voucherFlowInfo.setSendingDate(new Date());
                voucherFlowInfo.setErrMsg("");
            }else{
                voucherFlowInfo.setErrMsg(response.getMsg());
            }
            voucherFlowInfoService.updateById(voucherFlowInfo);
        return null ;
    }

    @Override
    public IRequest getRequestParams(VoucherItemRequest request) {
            return request.get();
    }
    @Override
    public VoucherResponse processRsp(JSONObject jsonObject) {
        VoucherResponse response = JSONObject.toJavaObject(jsonObject,VoucherResponse.class);
        return  VoucherResponse.convertFrom(response);
    }

    @Override
    public JSONObject send(IRequest iRequest) {
        return voucherItemApiFeign.send((VoucherItemDto) iRequest.getData());
    }
}
